var windowName = "main";
var Page,browser,load = 0;

var winName;
var urls;
var ajpush;
var frames = [];
apiready = function(){
    api.setStatusBarStyle({
        style: 'dark',
        color:"#FFF"
    });
    // api.setFullScreen({
    //     fullScreen: true
    //     // fullScreen: false
    // });
    //
    // ajpush = api.require('ajpush');
    //
    // ajpush.init(function(ret) {
    //     if (ret && ret.status){
    //         //success
    //         var param = {alias:'p1',tags:["user"]};
    //         ajpush.bindAliasAndTags(param,function(ret) {
    //             var statusCode = ret.statusCode;
    //         });
    //     }
    // });

    //监听urlScheme
    api.addEventListener({
        name:'appintent'
    },function(ret,err){
        var appParam = ret.appParam;
        logJson(appParam,"appParam:")
        if(api.systemType == 'ios'){
            var iosUrl = ret.iosUrl;
            logJson(iosUrl,"iosUrl:")
        } else {
            var sourceAppId = ret.sourceAppId;
            logJson(sourceAppId,"sourceAppId:")
        }
    });


    onWindowShow(function () {

        //获取版本信息存到本地
        var url = window.location.search;
        var version = url.substring(3);
        setLocData("version",version);
        log(version,"version:")

        //检查网络状态
        var cType = api.connectionType;
        log(cType,"网络状态")
        log(load,"load")

        //主页重回前台时调用相应函数
        if (load > 0){
            updateFrameData(Page.index)
        }
        load++
        setInterval(function () {
            load ++
        },2000 )


    })
    onWindowHide(function () {
    })

    var cookie = getLocData("cookie");
    browser = api.require('webBrowser');
    Page = createVM("#Page",{
        //页面数据
        index:0,
        tab:"index",
        shift:'home',
        showSite:false,
        open:false,
        test:""
    },{
        //页面方法
        "init":function () {

            var rnd = "?t=1.01";
            winName = ["index1","index2","index3","index4","index5"];
            urls = [
                pageUrl + "/ApiCloud/Page/User/index.html"+rnd,
                pageUrl + "/ApiCloud/Page/User/task.html"+rnd,
                pageUrl + "/ApiCloud/Page/User/pub.html"+rnd,
                pageUrl + "/ApiCloud/Page/User/match.html"+rnd,
                pageUrl + "/ApiCloud/Page/User/user.html"+rnd
            ];
            logJson(urls)

            for (var i = 0,len = urls.length; i < len; i++) {
                frames.push( {
                    // name: 'frame'+i,
                    name:winName[i],
                    url: urls[i],
                    bgColor : 'rgba(0,0,0,.0)',
                    bounces:false
                } )
            }
            funIniGroup();
            // getConfig();
        }
        ,closeBottomSheet:function (value) {
            this.open = false;

        }
        ,change:function (value) {
            log(value,"value:")
            // if (value == 'index3') {
            //     this.open = true;
            //     return false;
            // }
            var index = winName.indexOf(value);
            this.index = index;
            log(index,"index:")
            updateFrameData(index)
            api.setFrameGroupIndex({name: 'group', index:index });

        }
    })
    Page.init();




}

function funIniGroup(){
    var offset = $api.offset( document.getElementById('footer'));
    // Page.test = offset.t
    // Page.test += "-"+ offset.l
    // Page.test += "-"+ api.winHeight
    // showToast(offset.t)
    api.openFrameGroup({
        name: 'group',
        scrollEnabled: false,
        rect: {
            x: 0,
            y: 0,
            w: api.winWidth,
            // h: $api.dom('#main').offsetHeight
            h: api.winHeight * 0.92
        },
        index: 0,
        frames: frames,
        preload:5,
        fixedOn:"ui_window"
    }, function (ret, err) {
        var name =  ret.name;
        var index =  winName.indexOf(name)
        // Page.tab = "index" + (ret.index + 1)
        Page.tab = winName[index]
        Page.index = index
        logJson(ret)
        load++
    });
}

function updateFrameData(index) {
    api.setScreenOrientation({orientation: 'auto_portrait'});
    setTimeout(function () {
        var jsfun = 'updateWinData();';
        api.execScript({
            frameName: winName[index],
            script: jsfun
        });
    },300);
    //登录后需要更新数据
    log("检查登录","testLogin:")
    if (isTrue(getLocData("login")) && !isTrue(Page.login)) {
        log("发现用户登录")
        // getConfig()
    }
}

function getConfig() {
    apiGet("/app/getConfig",{},function (res) {
        if (isTrue(res.d.myGrade)) {setLocData("myGrade",{id:res.d.myGrade,name:res.d.gradeName[res.d.myGrade]})}
        setLocData("grade",res.d.grade);
        setLocData("subject",res.d.subject);
        setLocData("grades",res.d.gradeName);
        setLocData("subjects",res.d.subjectName);
        setLocData("uid",res.d.uid);
        setLocData("login",res.d.login);
        setLocData("qrCode",res.d.qrCode);
        setLocData("contactPhone",res.d.contactPhone);
        setLocData("chargeConfig",res.d.chargeConfig);
        setLocData("headPic",res.d.headPic);
        setLocData("phone",res.d.phone);
        setLocData("conf",res.d.conf);
        log(res)
        Page.login = getLocData("login")

        var param = {alias:'p'+getLocData("uid"),tags:["user"]};
        ajpush.bindAliasAndTags(param,function(ret) {
            var statusCode = ret.statusCode;
        });
    })
}


function tabTo(index) {
    Page.change("index" + index)
}
